<template>
	<div>
	  <div class="postpay">
	    <div class="add-des">请上传支付凭证</div>
	    <div class="add-list">
	    	<div class="add-img" @click="chooseImage">
	    		<div style="margin-top: .3rem;"><span class="icon-tianjia iconfont"></span></div>
	    		<div>添加图片</div>
	    	</div>
	    	<div class="add-img close" v-for="(item,index) in imgs">
	    		<i class="icon-bclose iconfont" @click="remImg(index)"></i>
	    		<div><img :src="item"></div>
	    	</div>
	    	<p style="clear:both"></p>
	    </div>
	  </div>
	  <div class="bp-submit" @click="createOrder()" style="width:35%">提交</div>
	</div>
</template>
<script>
  import Vue from 'vue'
  // import wx from 'weixin-js-sdk'
  // import Lockr from 'lockr'
  // import WxUtils from '@/common/js/common/WxUtils'
  export default {
    name: 'postpay',
    data () {
      return {
        i: 0,
        imgs: [],
        localIds: [],
        serverId: [],
        isclick: true
      }
    },
    created: function () {
      // WxUtils.init()
    },
    mounted: function () {
      this.$tools.loading(false)
    },
    methods: {
      // upSubmit: function () {
      //   let obj = this.contentObj
      //   let _this = this
      //   this.$EcHttpUtils.post('comment/submit.json', obj).then(function (resp) {
      //     if (resp.data.statusCode === 0) {
      //       _this.$layer.toast({
      //         icon: 'icon-check', // 图标clssName 如果为空 toast位置位于下方,否则居中
      //         content: '提交成功',
      //         time: 2000 // 自动消失时间 toast类型默认消失时间为2000毫秒
      //       })
      //       history.go(-1)
      //     }
      //   })
      // },
      remImg: function (index) {
        let _this = this
        let imgs = _this.imgs
        imgs.splice(index, 1)
      },
      chooseImage: function () {
        let _this = this
        // WxUtils.init()
        // alert(JSON.stringify(_this.$WxUtils))
        _this.$WxUtils.chooseImage(function (res) {
          // alert(JSON.stringify(res))
          _this.uploadImg(res)
        })
      },
      uploadImg: function (localId) {
        let _this = this
        let length = localId.length
        let i = this.i
        _this.$WxUtils.uploadImage(localId[i], function (serverId) {
          i++
          _this.serverId.push(serverId)
          _this.i = i
          _this.upImg(serverId)
          if (i < length) {
            _this.uploadImg(localId)
          }
        })
      },
      upImg: function (serverId) {
        let _this = this
        let arry1 = _this.imgs
        this.$EcHttpUtils.postForm(_this.$Constants.CommonApiUrl.WECHAT + '/js/uploadImg.json', {'serverId': serverId}).then(function (resp) {
          if (resp.data.statusCode === 0) {
            let img = resp.data.data.url
            arry1.push(img)
            // console.log(1111)
            // console.log(arry1)
            _this.i = 0
          }
        })
      },
      createOrder: function () {
        let _this = this
        if (Vue.prototype.$payWay !== 100) {
          alert('请在微信环境中打开')
          return false
        }
        if (this.imgs.length <= 0) {
          alert('请上传支付凭证')
          return false
        }
        if (!_this.isclick) {
          return false
        }
        let obj = this.$Lockr.get('PostPay')
        obj.posBill = JSON.stringify(this.imgs)
        // console.log(obj)
        this.$EcHttpUtils.post('order/createOrder.json', obj).then(function (resp) {
          console.log(resp.data)
          if (resp.data.statusCode === 0) {
            _this.isclick = false
            let data = resp.data.data.payInfoParam
            _this.$router.replace({name: 'Success', query: {status: 'SUCCESS', orderNo: data.orderNo, payAmount: obj.payAmount}})
            localStorage.removeItem('PostPay')
            // _this.$Constants.CommonApiUrl.payPage
            // _this.$layer.toast({
            //   icon: 'icon-check', // 图标clssName 如果为空 toast位置位于下方,否则居中
            //   content: '提交成功',
            //   time: 1500 // 自动消失时间 toast类型默认消失时间为2000毫秒
            // })
            // setTimeout(function () {
            //   _this.$router.go(-1)
            // }, 1800)
          } else {
            _this.$layer.toast({
              icon: 'icon-check', // 图标clssName 如果为空 toast位置位于下方,否则居中
              content: resp.data.message,
              time: 2000 // 自动消失时间 toast类型默认消失时间为2000毫秒
            })
          }
        })
      }
    }
  }
</script>
<style>
	
</style>
